package com.century.sjt.widget.TitleBar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.century.sjt.R;

/**
 * Created by 炳基 on 2016/7/7.
 * TitleBar
 */
public class HomeTitleBar extends LinearLayout {
    protected LinearLayout leftLayout;
    protected ImageView leftImage;
    private TextView rightIcon;
    protected TextView titleView;
    protected LinearLayout titleLayout;
    protected LinearLayout rightLayout;

    public HomeTitleBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public HomeTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public HomeTitleBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs){
         LayoutInflater.from(context).inflate(R.layout.sjt_home_titlebar, this);
        leftLayout = (LinearLayout) findViewById(R.id.left_layout);
        leftImage = (ImageView) findViewById(R.id.profile_image);
        rightLayout = (LinearLayout) findViewById(R.id.right_layout);
        rightIcon = (TextView) findViewById(R.id.right_icon);
        titleView = (TextView) findViewById(R.id.title);
        titleLayout = (LinearLayout) findViewById(R.id.root);
        Typeface iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
        rightIcon.setTypeface(iconfont);
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs){
        if(attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HomeTitleBar);
            String title = ta.getString(R.styleable.HomeTitleBar_HometitleBarTitle);
            titleView.setText(title);

            String imgUrl = ta.getString(R.styleable.HomeTitleBar_HometitleBarLeftImage);
            if (null != imgUrl) {
//                leftImage.setImageDrawable(leftDrawable);
                Glide.with(context).load(imgUrl).into(leftImage);
            }

            Drawable background = ta.getDrawable(R.styleable.HomeTitleBar_HometitleBarBackground);
            if(null != background) {
                titleLayout.setBackgroundDrawable(background);
            }


            String icon = ta.getString(R.styleable.HomeTitleBar_HometitleBarRightIcon);
            if (null !=icon){
                rightIcon.setText(icon);
            }


            ta.recycle();
        }
    }

    public void setLeftImageResource(Context context,String imgUrl) {
//        leftImage.setImageResource(resId);
        Glide.with(context).load(imgUrl).into(leftImage);
    }

    public void setRightIconResourc(String resId) {
        rightIcon.setText(resId);
    }
    public void setLeftLayoutClickListener(OnClickListener listener){
        leftLayout.setOnClickListener(listener);
    }

    public void setRightLayoutClickListener(OnClickListener listener){
        rightLayout.setOnClickListener(listener);
    }
    public void setTitle(String title){
        titleView.setText(title);
    }
    public void setBackgroundColor(int color){
        titleLayout.setBackgroundColor(color);
    }

    public LinearLayout getLeftLayout(){
        return leftLayout;
    }

    public LinearLayout getRightLayout(){
        return rightLayout;
    }
}
